
let tableIns;
let tree;
layui.use(['table', 'form','layer', 'laydate', 'tree', 'util'], function () {
    let table = layui.table , form = layui.form, curl = window.location.href;
    tree = layui.tree;
    let height = document.documentElement.clientHeight - 60;


    tableIns = table.render({
        elem: '#grantInfoTable'
        , url: '/platform/grant/list/page'
        , method: 'POST'
        //请求前参数处理
        , request: {
            pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'rows' //每页数据量的参数名，默认：limit
        }
        , response: {
            statusName: 'flag' //规定数据状态的字段名称，默认：code
            , statusCode: true //规定成功的状态码，默认：0
            , msgName: 'msg' //规定状态信息的字段名称，默认：msg
            , countName: 'records' //规定数据总数的字段名称，默认：count
            , dataName: 'rows' //规定数据列表的字段名称，默认：data
        }
        //响应后数据处理
        , parseData: function (res) { //res 即为原始返回的数据
            var data = res.data;
            console.log("data=="+res.data.rows[0].personName);
            return {
                "flag": res.flag, //解析接口状态
                "msg": res.msg, //解析提示文本
                "records": data.records, //解析数据长度
                "rows": data.rows //解析数据列表
            };
        }
        , title: '发放管理'
        , cols: [[
            {field: 'id', title: 'id', hide: true}
            , {field: 'orderNo', title: '订单号'}
            , {field: 'applyTime',title: '申请时间'}
            , {field: 'doneTime',title: '完成时间'}
            , {field: 'companyName', title: '商户名称'}
            , {field: 'serviceCenter', title: '服务商名称'}
            , {field: 'grantAmount', title: '实放金额'}
            , {field: 'bizContent', title: '业务处理情况'}
            , {title: '订单状态' ,toolbar: '#staBar'}
            , { title: '发放' ,toolbar: '#grantBar'}
            ,{title: '操作' ,toolbar: '#downLoadBar'}
        ]]
        , defaultToolbar: ['', '', '']
        , page: true
        , height: height
        , cellMinWidth: 80
    });

    table.on('tool(display)',function (obj) {
        var _data = obj.data;
        console.log(_data);
        console.log("id====="+_data);
        if(obj.event === 'grantBar'){
            layer.open({
                type: 2,
                title: '发放设置',
                closeBtn: 1, //显示关闭按钮
                anim: 2,
                area: ['800px', '450px'],
                shadeClose: true, //开启遮罩关闭
                content: '/platform/grant/grantInfo?platformGrantId='+data.id,
                success: function(layero, index){

                }
            });
        }
        if(obj.event === 'downLoad'){
            $.get('/platform/grant/export',{platformGrantId: _data.id},function (res) {
                console.log(res);
            });
        }
    });

    form.on('select(editSta)',function (obj) {
        var _val = obj.value,
            _status = _val.split('|')[0],
            _id = _val.split('|')[1];
        $.post("/platform/grant/update", {orderNo:_id,orderStatus:_status}, function (res) {
            var data = JSON.parse(res);
            console.log("flag=="+res);
            console.log("data=="+data);
            if(data.flag){
                layer.msg("修改成功", {icon: 1, time: 2000}, function(){
                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , done: function (res, curr, count) {
                            //完成后重置where，解决下一次请求携带旧数据
                            this.where = {};
                        }
                    }, 'data');
                });
            } else {
                layer.msg("修改失败!", {icon: 5});
            }
        });
    });

});


function formatMoney(str) {
    if (!str) str = '0.00';
    let ret = Math.round(parseFloat(str) * 100) / 100;
    let decimal = ret.toString().split('.');
    if (decimal.length === 1) {
        return ret.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")
            + '.00';
    };
    if (decimal.length > 1) {
        decimal[0] = decimal[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        if (decimal[1].length < 2) {
            decimal[1] += '0';
            return decimal.join(".");
        }
        return decimal.join(".");
    };
    return ret;
}